//
//  LLWatchHistoryViewController.m
//  LoveLove
//
//  Created by 达迩科技 on 2019/4/10.
//  Copyright © 2019 wkf. All rights reserved.
//

#import "LLWatchHistoryViewController.h"
#import "LLboughtContentTableViewCell.h"

@interface LLWatchHistoryViewController ()<UINavigationControllerDelegate>



@property (nonatomic,strong) NSMutableArray *dataArray;

@end

@implementation LLWatchHistoryViewController

-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)viewWillAppear:(BOOL)animated {
    [[IQKeyboardManager sharedManager] setEnable:NO];
    [super viewWillAppear:animated];
    [SVProgressHUD dismiss];
    self.navigationController.delegate = self;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    //TODO: 页面Disappear 启用
    [[IQKeyboardManager sharedManager] setEnable:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
}

#pragma mark - 显示视图操作方法
- (void)configTableView {
    self.needRefresh = NO;
    [super configTableView];
    [self titleView];
    self.titleView.backgroundColor = LLColorRGB(255);
    self.titleLabel.text = @"观看历史";
    self.rightButton.hidden = YES;
    self.tableView.backgroundColor = LLColorRGB(255);
    self.tableView.top = self.navH;
    self.tableView.mj_h = LLScreenHeight-self.navH;
    [self.tableView registerCellByClassName:@"LLboughtContentTableViewCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [self sendRecordRequst];
    }];

    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page= 1;
        [self.dataArray removeAllObjects];
        [self sendRecordRequst];
    }];
    [self.tableView.mj_header beginRefreshing];
    
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LLboughtContentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LLboughtContentTableViewCell"];
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;//无高亮选中
    cell.backgroundColor = [UIColor whiteColor];
    NSDictionary *dic = self.dataArray[indexPath.row];
    [cell configCellData:dic];
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.0000001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.0000001;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark ---- dealloc
- (void)dealloc {
    [LLNotificationCenter removeObserver:self];
}

#pragma mark - UINavigationControllerDelegate
// 将要显示控制器
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 判断要显示的控制器是否是自己
    BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
    [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
}
#pragma mark---观看历史
-(void)sendRecordRequst{
    LLUserInfo *user = [LLUserInfo sharedUser];
    
    NSDictionary *dic = @{@"edu_id":@"21",@"user_random":user.user_random,@"user_id":user.user_id,@"page":@(self.page)};
    [LLHttpTool post:LLHOST headParams:nil params:dic success:^(NSDictionary *result) {
        self.page++;
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        NSArray *dataArr = result[@"data"];
        if (dataArr.count == 0) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            return ;
        }
        [self.dataArray addObjectsFromArray:dataArr];
        [self.tableView reloadData];
    } failure:^(NSString *msg) {
        [SVProgressHUD showErrorWithStatus:msg];
    }];
    
}

@end
